// pages/myapiary/edit/edit.js
import http from '../../../http/http.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province: [],
    city: [],
    honeyTypelist: [],
    areachoice: false,
    provinceStr: '',
    cityStr: '',
    pcastr: null,
    ctindex: 0,
    activeindex: -1,
    pars: {
      name: '',
      province: '',
      city: '',
      address: '',
      ownerContact: '',
      beeType: '',
      honeyType: '',
      descriptions: '',
      lat: '',
      lng: '',
      id:''
    },
    beeTypeName:'',
    ac_name: false,
    ac_address: false,
    ac_phone: false,
    ac_honey: false,
    ac_descript: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdata(options.id)
    this.setData({
      ['pars.id']: options.id
    })
    let _this = this
    this.initpc()
    this.beetype()
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        _this.setData({
          ['pars.lat']: res.latitude,
          ['pars.lng']: res.longitude,
        })
      },
    })
  },
  // 蜜蜂类型
  beetype: function () {
    http.getdata('/api/apiary/species/list').then((res) => {
      this.setData({
        honeyTypelist: res.data.result
      })
    })
  },
  getdata: function (arg) {
    http.getdata('/api/apiary/get?id=' + arg).then((res) => {
      let val = res.data.result.provinceName + ' ' + res.data.result.cityName
      this.setData({
        pcastr: val,
        ['pars.name']: res.data.result.name,
        ['pars.province']: res.data.result.province,
        ['pars.city']: res.data.result.city,
        ['pars.address']: res.data.result.address,
        ['pars.ownerContact']: res.data.result.ownerContact,
        ['pars.beeType']: res.data.result.beeType,
        ['pars.honeyType']: res.data.result.honeyType,
        ['pars.descriptions']: res.data.result.descriptions,
        ['pars.lat']: res.data.result.lat,
        ['pars.lng']: res.data.result.lng,
        beeTypeName: res.data.result.beeTypeName
      })
    })
  },
  // 初始化省市
  initpc: function () {
    http.getdata('/api/region/list?parentId=0').then((res) => {
      this.setData({
        province: res.data.result,
        ['pars.province']: res.data.result[0].id
      })
      http.getdata('/api/region/list?parentId=' + res.data.result[0].id).then((ctityres) => {
        this.setData({
          city: ctityres.data.result,
          ['pars.city']: ctityres.data.result[0].id
        })
      })
    })
  },
  // 省
  bindChange: function (e) {
    const val = e.detail.value
    let index = e.detail.value[0]
    let cityindex = e.detail.value[1]
    let proviceid = this.data.province[index].id
    console.log(e)
    http.getdata('/api/region/list?parentId=' + proviceid).then((res) => {
      this.setData({
        city: res.data.result,
        ['pars.province']: proviceid,
        provinceStr: this.data.province[index].name,
        cityStr: this.data.city[cityindex].name,
        ctindex: e.detail.value[1]
      })
    })
  },
  // 定位
  position: function () {
    var _this = this
    wx.chooseLocation({
      success: function (res) {
        _this.setData({
          ['pars.address']: res.address,
          ['pars.lat']: res.latitude,
          ['pars.lng']: res.longitude,
        })
      },
    })
  },
  // 获取
  getvalue: function (e) {
    let type = e.currentTarget.dataset.type
    let val = e.detail.value
    if (type == 'name') {
      this.data.pars.name = val
    } else if (type == 'address') {
      this.data.pars.address = val
    } else if (type == 'phone') {
      this.data.pars.ownerContact = val
    } else if (type == 'honey') {
      this.data.pars.honeyType = val
    } else {
      this.data.pars.descriptions = val
    }
  },
  active: function (e) {
    let type = e.currentTarget.dataset.type
    let val = e.detail.value
    if (type == 'name') {
      this.setData({
        ac_name: true,
        ac_address: false,
        ac_phone: false,
        ac_honey: false,
        ac_descript: false,
      })
    } else if (type == 'address') {
      this.setData({
        ac_name: false,
        ac_address: true,
        ac_phone: false,
        ac_honey: false,
        ac_descript: false,
      })
    } else if (type == 'phone') {
      this.setData({
        ac_name: false,
        ac_address: false,
        ac_phone: true,
        ac_honey: false,
        ac_descript: false,
      })
    } else if (type == 'honey') {
      this.setData({
        ac_name: false,
        ac_address: false,
        ac_phone: false,
        ac_honey: true,
        ac_descript: false,
      })
    } else {
      this.setData({
        ac_name: false,
        ac_address: false,
        ac_phone: false,
        ac_honey: false,
        ac_descript: true,
      })
    }
  },
  
  // 类型选择
  bindPickerChange: function (e) {
    let val = e.detail.value
    this.setData({
      ['pars.beeType']: this.data.honeyTypelist[val].code,
      activeindex: val
    })
  },

  sure_area: function () {
    var value = this.data.provinceStr + " " + this.data.city[this.data.ctindex].name
    this.setData({
      pcastr: value,
      areachoice: false
    })
   
  },
  // 取消
  canel: function () {
    this.setData({
      areachoice: false
    })
  },
  // 确认
  sure: function () {
    this.initpc()
    this.setData({
      areachoice: true,
      ctindex:0
    })
  },
  // 提交
  submit: function () {
    this.data.pars.city = this.data.city[this.data.ctindex].id
    console.log(this.data.pars)
    if (this.data.pars.name == '') {
      wx.showToast({
        title: '请输入蜂场名称',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pcastr == '' || this.data.pcastr == null) {
      wx.showToast({
        title: '请选择蜂场区域',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.address == '') {
      wx.showToast({
        title: '详细地址不能为空',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.ownerContact == '') {
      wx.showToast({
        title: '请输入管理员电话',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.beeType == '') {
      wx.showToast({
        title: '请选择蜜蜂品种',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.honeyType == '') {
      wx.showToast({
        title: '请输入蜜源品种',
        icon: 'none',
        mask: true
      })
      return false
    } else if (this.data.pars.descriptions == '') {
      wx.showToast({
        title: '请输入蜂场简介',
        icon: 'none',
        mask: true
      })
      return false
    } else {
      http.putdata('/api/apiary/edit', this.data.pars).then((res) => {
        wx.redirectTo({
          url: '../list/myapiary',
        })
      })
    }
  }
})